CREATE procedure amsp_ICMove
@InSourceNodeID numeric,
@InDestNodeID numeric
AS
BEGIN
Declare
@DestSortOrder numeric,
@DestDepth numeric,
@DestParentID numeric,
@DestAncestorID numeric,
@SourceSortOrder numeric,
@SourceDepth numeric,
@SourceParentID numeric,
@SourceAncestorID numeric,
@InvalidCount int,
@Counter int
IF @InSourceNodeID != @InDestNodeID BEGIN
SET @InvalidCount = 0
BEGIN TRANSACTION
SELECT
@DestSortOrder = SortOrder,
@DestDepth = CategoryDepth,
@DestParentID = ParentCategoryID,
@DestAncestorID = AncestorCategoryID
FROM
Interest_Category
WHERE
InterestCategoryID = @InDestNodeID
SELECT
@SourceSortOrder = SortOrder,
@SourceDepth = CategoryDepth,
@SourceParentID = ParentCategoryID,
@SourceAncestorID = AncestorCategoryID
FROM
Interest_Category
WHERE
InterestCategoryID = @InSourceNodeID
IF @DestAncestorID = @SourceAncestorID BEGIN
SET @Counter = 0
CREATE TABLE #TEMP (ChildID numeric)
INSERT INTO #TEMP
SELECT InterestCategoryID
FROM Interest_Category
WHERE ParentCategoryID = @InSourceNodeID
WHILE (@Counter < 50 AND @@ROWCOUNT > 0) BEGIN
SET @Counter = @Counter + 1
INSERT INTO #TEMP
SELECT InterestCategoryID
FROM Interest_Category
WHERE ParentCategoryID IN (SELECT ChildID FROM #TEMP)
AND InterestCategoryID NOT IN (SELECT ChildID FROM #TEMP)
END
SELECT @InvalidCount = Count(*)
FROM #TEMP
WHERE ChildID = @InDestNodeID
IF @InvalidCount > 0 BEGIN
RAISERROR ('You may not move nodes within the same level 1 hierarchy', 16, 1)
END
END
IF @InvalidCount = 0 BEGIN
UPDATE
Interest_Category
SET
ParentCategoryID = @DestParentID,
SortOrder = @DestSortOrder + 1
WHERE
InterestCategoryID = @InSourceNodeID
EXEC amsp_ICFixTree
END
COMMIT TRANSACTION
END
END
GO
GRANT EXECUTE ON [dbo].[amsp_ICMove] TO [IMIS]
GO